package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.QueryObject.StrategyCommentQuery;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.exception.LogicException;
import cn.wolfcode.luowowo.respository.StrategyCommentRespository;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import javafx.fxml.LoadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时不管事务
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRespository strategyCommentrRespository;

    @Autowired
    private IDestinationService destinationService;


    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public StrategyComment get(String id) {
        Optional<StrategyComment> optional = strategyCommentrRespository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public List<StrategyComment> list() {
        return strategyCommentrRespository.findAll();
    }

    @Override
    public void addComment(StrategyComment strategyComment) {
        if (strategyComment != null) {
            strategyComment.setId(null);
            strategyComment.setCreateTime(new Date());
            strategyCommentrRespository.save(strategyComment);
        } else {
            throw new LogicException("添加攻略评论失败");
        }
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getStrategyId())) {
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }
        PageRequest of = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "thumbupnum");
        qo.setPageable(of);
        return DBHelper.query(mongoTemplate, StrategyComment.class, query, qo.getPageable());
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //判断是点赞还是取消点赞

        //1.调用该类中的方法根据id获取评论
        StrategyComment strategyComment = this.get(cid);
        //2.获取评论中的点赞人集合
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //3.判断点赞还是取消点赞
        if (thumbuplist.contains(uid)) {
            //包含 即已点过赞
            thumbuplist.remove(uid);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
        } else {
            //不包含 即未过赞
            thumbuplist.add(uid);
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
        }
        //4.更新
        strategyCommentrRespository.save(strategyComment);
    }

}
